package com.yanqu.road.server.manager.activity.xingyou.task;

import com.yanqu.road.entity.activity.commonteam.data.CommonTeamUserData;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiUserData;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.xingyou.CrossXingYouActivity;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.property.Property;

import java.util.Collections;
import java.util.List;

public class CrossXingYouSendWishShareRewardTask extends GeneralTask {


    private long playerId;

    private int teamId;

    private List<CommonTeamUserData> memberList;

    private Property reward;

    private CrossXingYouActivity activity;

    public CrossXingYouSendWishShareRewardTask(long playerId, int teamId, List<CommonTeamUserData> memberList, Property reward, CrossXingYouActivity activity) {
        this.playerId = playerId;
        this.teamId = teamId;
        this.memberList = memberList;
        this.reward = reward;
        this.activity = activity;
    }

    @Override
    public void doRun() {

        int goldCoin = reward.getCountByGoodsId(activity.getConfig().getSHANSHUI_GOLD_COIN_ID()).intValue();
        synchronized (activity.getTeamLocker(teamId)) {
            for (CommonTeamUserData userData : memberList) {
                CrossXingYouShanShuiUserData shanShuiUserData = activity.getUserData(userData.getUserId());
                //展示用
                if(userData.getUserId() != playerId) {
                    shanShuiUserData.setWishShareValue(shanShuiUserData.getWishShareValue() + goldCoin);
                }
            }
        }
        if(goldCoin > 0) {
            //下发金页子奖励
            for (CommonTeamUserData userData : memberList) {
                if(userData.getUserId() == playerId){
                    continue;
                }
                XingYouShanShuiProto.CrossShanShuiAddGoldCoinReq.Builder sync = XingYouShanShuiProto.CrossShanShuiAddGoldCoinReq.newBuilder();
                sync.setNum(goldCoin);
                sync.setWay(eLogMoneyType.XingYouShanShuiWishShareReward.getValue());
                MessageHelper.sendPacket(userData.getServerId(), userData.getUserId(), YanQuMessageUtils.buildMessage(Protocol.S_XING_YOU_SHAN_SHUI_ADD_GOLD_COIN_FROM_CROSS, sync));
            }
        }

        //activity.syncShanShuiPropNotify(teamId, Collections.singletonList(XingYouShanShuiProto.PROP.QI_YUAN_PROGRESS_VALUE));

        activity.getNeedSyncRedDotTeamMap().add(teamId);
    }
}
